package com.wine.exam.controller;


import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Pattern;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wine.common.annotation.RepeatSubmit;
import com.wine.exam.dto.QuestionDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.wine.common.annotation.Log;
import com.wine.common.core.controller.BaseController;
import com.wine.common.core.domain.AjaxResult;
import com.wine.common.enums.BusinessType;
import com.wine.exam.domain.Question;
import com.wine.exam.service.IQuestionService;
import com.wine.common.utils.poi.ExcelUtil;
import com.wine.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 题目Controller
 *
 * @author wine
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/exam/question")
@Validated
public class QuestionController extends BaseController {
    @Autowired
    private IQuestionService QuestionService;

    /**
     * 查询题目列表
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:question:index')")
    @GetMapping("/list")
//    关于参数获取问题：这里没用注解，学得若依，但是subject哪里些了注解 @RequestParam
    public AjaxResult list(QuestionDto questionDto) {

        Page<Question> page = QuestionService.pageQueryList(questionDto);
        return AjaxResult.success(page);
    }

    /**
     * 导出题目列表
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:question:export')")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionDto questionDto) {
        Page<Question> page = QuestionService.pageQueryList(questionDto);
        List<Question> records = page.getRecords();
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, records, "题目数据");
    }

    /**
     * 获取题目详细信息
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:question:query')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") Long questionId) {
        Question info = QuestionService.getInfo(questionId);
        return AjaxResult.success(info);
    }

    /**
     * 新增题目
     * 缺陷：有任意一个权限都行， 后续在考虑是不是细化。
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:question:add," +
            "exam:question:add:shortAnswer," +
            "exam:question:add:judge," +
            "exam:question:add:singleChoice," +
            "exam:question:add:multipleChoice')")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Question question) {
//        TODO :可以在basicController里设置一个方法来，给参数，设置这些后返回了设置的
//          :这里以后走多个controller，但是方法还是同一个，来做很细的权限时，可以用这个
        question.setCreateBy(getUserId().toString());
        question.setUpdateBy(getUserId().toString());
        return toAjax(QuestionService.add(question));

    }

    /**
     * 修改题目
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:question:edit')")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Question question) {
//        return toAjax("QuestionService.updateQuestion(Question)");
        return toAjax(QuestionService.edit(question));
    }

    /**
     * 修改 设置status
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:subject:edit')")
    @Log(title = "问题状态", businessType = BusinessType.UPDATE)
    @PostMapping("/editStatus/{questionId}/{status}")
    public AjaxResult editStatus(@PathVariable Long questionId,
                                 @Pattern(regexp = "[0,1]", message = "问题状态:0=正常,1=停用")
                                 @PathVariable String status) {
//        return toAjax(
//                " QuestionService.updateStatus(questionId, status)"
//        );
        return toAjax(QuestionService.updateStatus(questionId, status, getUserId().toString()));
    }


    /**
     * 删除题目
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:question:remove')")
    @Log(title = "题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds) {
//        return toAjax('QuestionService.deleteQuestionByIds(questionIds)');
        return toAjax(QuestionService.delByIds(questionIds));
    }
}
